// pages/dev_list/dev_lsit.js

  /**
   * 生命周期函数--监听页面加载
   */// pages/index/index.js
  var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
  var app = getApp();
  Page({
    /**
     * 页面的初始数据
     */
    data: {
      city: "",
      weather: { 'wea_img': 'qing' },//实况天气
      devID_list: [{ devNo:"868221047652659", nick:"设备一"}, { devNo:"868221047652659", nick:'设备二'}, { devNo:"868221047652659", nick:'设备三'}, { devNo:"868221047652659", nick:'设备四'}, { devNo:"868221047652659", nick:'设备五'}],
      list: [
        {
          id: 'view',
          name: '历史记录',
          open: true,
          pages: [{ devNo: '868221047652659', nick: "设备一", description: "test dev111", status: "online" }, { devNo: '868221047652659', nick: '设备二', description: "test dev111", status: "online" }, { devNo: '868221047652659', nick: '设备三', description: "test dev111", status: "online" }, { devNo: '868221047652659', nick: '设备四', description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: '设备五', description: "test dev111", status: "online" }, { devNo: '868221047652659', nick: "设备六", description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: "设备七", description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: "设备八", description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: "设备九", description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: "设备十", description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: "设备十一", description: "test dev111", status: "un_online" }, { devNo: '868221047652659', nick: "设备十二", description: "test dev111", status: "online" }, { devNo: '868221047652659', nick: "设备十三", description: "test dev111", status: "online" }, { devNo: '868221047652659', nick: "设备十四", description: "test dev111", describe: "test dev111", status: "online" }, { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },
            { devNo: '868221047652659', nick: "设备十五", description: "test dev111", status: "online" },

          ]
          // pages:[]
        }
      ],
      listData: [
        { "code": "01", "text": "text1", "type": "type1" },
        { "code": "02", "text": "text2", "type": "type2" },
        { "code": "03", "text": "text3", "type": "type3" },
        { "code": "04", "text": "text4", "type": "type4" },
        { "code": "05", "text": "text5", "type": "type5" },
        { "code": "06", "text": "text6", "type": "type6" },
        { "code": "07", "text": "text7", "type": "type7" }
      ]
      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      //获取当前天气
      let that = this;

      //把登录时获取到的设备列表 赋值给 本地 pages


      //this.getapi();
      var header = {
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': "openid=" + app.globalData.openid
      };

      

      wx.request({
        url: 'https://119.3.46.145/server/FindDevice?userid=' + app.globalData.userid,
        method: 'POST',
        header: header,
        success: res => {         
          let dev_arr = JSON.stringify(res.data.devices);
          that.setData({
            page:dev_arr
          });

        },
        fail: function (error) {
          console.log(error, "FindDevice error");
        },
        complete: function (complete) {

        }
      })      
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // let that = this;
    //调用定位方法
    //that.getUserLocation();
    var that = this;
  },

  //定位方法
  getUserLocation: function () {
    var _this = this;
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          //未授权
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                //取消授权
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                //确定授权，通过wx.openSetting发起授权请求
                wx.openSetting({
                  success: function (res) {
                    if (res.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      _this.geo();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //用户首次进入页面,调用wx.getLocation的API
          _this.geo();
        }
        else {
          //console.log('授权成功')
          //调用wx.getLocation的API
          _this.geo();
        }
      }
    })

  },

  // 获取定位城市
  geo: function () {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        // wx.request({
        //   url: 'http://api.map.baidu.com/geocoder/v2/?ak=xxxxxxxxxxxx&location=' + res.latitude + ',' + res.longitude + '&output=json',
        //   data: {},
        //   header: { 'Content-Type': 'application/json' },
        //   success: function (ops) {
        //     console.log('定位城市：', ops.data.result.addressComponent.city)
        //   },
        //   fail: function (resq) {
        //     wx.showModal({
        //       title: '信息提示',
        //       content: '请求失败',
        //       showCancel: false,
        //       confirmColor: '#f37938'
        //     });
        //   },
        //   complete: function () {
        //   }
        // })

        var qqmapsdk = new QQMapWX({
          key: 'BYXBZ-763KX-YTT4S-7SEOC-N5I23-I5BAE'
        });

        wx.getLocation({  //获取当前地址
          type: 'wgs84',
          success: function (res) {
            var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
            var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180
            //根据经纬度获取所在城市
            qqmapsdk.reverseGeocoder({
              location: { latitude: latitude, longitude: longitude },
              success: function (res) {
                //address 城市
                that.setData({ city: res.result.address_component.city })
                wx.showToast({
                  title: `当前位置： ` + that.data.city,
                  icon: 'none'
                });
              }
            });
          }
        });
      }
    })
  },

  //天气api
  getapi: function () {
    // let that = this;
    // // 获取IP地址
    // wx.request({
    //   url: 'https://tianqiapi.com/ip/',
    //   data: {
    //   },
    //   method: 'POST',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     console.log(res, "success res");
    //     // 根据IP获取天气数据
    //     that.weathertoday(res.data.ip);
    //   },
    //   fail: function (res) {
    //     console.log(res, "fail res")
    //   }

    // });
  },

  // 天气api实况天气
  weathertoday: function (ip) {
    let that = this;
    wx.request({
      url: 'https://tianqiapi.com/api/?version=v6',
      data: {
        'ip': ip
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          weather: res.data
        });
        //console.log(that.data.weather, "weather success");
      },
      fail: function (res) {
        //console.log(res, "weather fail");
      }
    });
  },

  goto_detail: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  },

  add_dev: function () {
    wx.navigateTo({
      url: '../add_dev/add_dev',
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})